package com.sdp.lease.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.alibaba.fastjson.JSONObject;

public class WxApi {

	 // 微信小程序的AppID和AppSecret
    private static final String APP_ID = "wx31163f7cbc75308a";
    private static final String APP_SECRET = "c88c42d1b6639184a6229426f162b66a";
    
    public static void main(String[] args) {
    	String res = getuserphonenumber("83300ee8ff3afa70716c5ddf5fe49795c826ec1b678c4f73508733f0b6b6e383");
    	JSONObject resObj = JSONObject.parseObject(res);
    	if("0".equals(resObj.getString("errcode"))) {
    		System.out.println(resObj.getJSONObject("phone_info").getString("phoneNumber"));
    	}
	}
    
    /**
     * 获取微信小程序token
     * @return
     */
    public static String getAccessToken(){
        String accessToken = null;
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APP_ID+"&secret="+APP_SECRET;

        try {
            URL accessTokenUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) accessTokenUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);

            InputStream stream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            StringBuilder builder = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            JSONObject jsonObject = JSONObject.parseObject(builder.toString());
            accessToken = jsonObject.get("access_token").toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return accessToken;
    }
    
    
   /**
    * 获取手机号
    * @param code
    */
    public static String getuserphonenumber(String code){
        String urlpath="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+getAccessToken();
        String res="";
        try {
            URL url=new URL(urlpath);
            try {
                HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty("content-type","application-json");
                // 设置连接超时时间为10秒
                httpURLConnection.setConnectTimeout(10000);
                // 设置读取超时时间为10秒
                httpURLConnection.setReadTimeout(10000);
                httpURLConnection.setDoOutput(true);

                JSONObject sendBody = new JSONObject();
                sendBody.put("code",code);
                OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream(),"UTF-8");
                writer.write(sendBody.toString());
                writer.flush();
                writer.close();
                int responseCode = httpURLConnection.getResponseCode();
                if(responseCode ==200){
                    BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                    String line;
                    StringBuilder response = new StringBuilder();
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
//                    System.out.println(response);
                    res = response.toString();
                    reader.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return res;
    }
}
